#include "led4x4.h"
volatile bool led4x4_buffer[4][4]; // 每行的缓存
void LED4x4_SetNumber(uint8_t number, bool value)
{
    switch (number)
    {
    case 15: 
        LED4x4_SetPixel(0, 0, value);
        break;
    case 14:
        LED4x4_SetPixel(0, 1, value);
        break;
    case 13:
        LED4x4_SetPixel(0, 2, value);
        break;
    case 12: 
        LED4x4_SetPixel(0, 3, value);
        break;
    case 11: 
        LED4x4_SetPixel(1, 3, value);
        break;
    case 10: 
        LED4x4_SetPixel(1, 2, value);
        break;
    case 9: 
        LED4x4_SetPixel(1, 1, value);
        break;
    case 8:
        LED4x4_SetPixel(1, 0, value);
        break;
    case 7:
        LED4x4_SetPixel(2, 0, value);
        break;
    case 6:
        LED4x4_SetPixel(2, 1, value);
        break;
    case 5: 
        LED4x4_SetPixel(2, 2, value);
        break;
    case 4: 
        LED4x4_SetPixel(2, 3, value);
        break;
    case 3:
        LED4x4_SetPixel(3, 3, value);
        break;
    case 2: 
        LED4x4_SetPixel(3, 2, value);
        break;
    case 1:
        LED4x4_SetPixel(3, 1, value);
        break;
    case 0: 
        LED4x4_SetPixel(3, 0, value);
        break;
    default:
        break;
    }
}

void LED4x4_SetPixel(uint8_t x, uint8_t y, bool value)
{
    if (x > 3 || y > 3)
        return;
    led4x4_buffer[x][y] = value;
}

void LED4x4_ClearAllPixels(void)
{
    for (uint8_t i = 0; i < 4; i++)
    {
        for (uint8_t j = 0; j < 4; j++)
        {
            led4x4_buffer[i][j] = 0;
        }
    }
}

void LED4x4_SetAllPixels(void)
{
    for (uint8_t i = 0; i < 4; i++)
    {
        for (uint8_t j = 0; j < 4; j++)
        {
            led4x4_buffer[i][j] = 1;
        }
    }
}
void LED4x4_Clear(void)
{
    DL_GPIO_clearPins(LED_GRP_LED_1_PORT, LED_GRP_LED_1_PIN);
    DL_GPIO_clearPins(LED_GRP_LED_2_PORT, LED_GRP_LED_2_PIN);
    DL_GPIO_clearPins(LED_GRP_LED_3_PORT, LED_GRP_LED_3_PIN);
    DL_GPIO_clearPins(LED_GRP_LED_4_PORT, LED_GRP_LED_4_PIN);
    DL_GPIO_setPins(LED_GRP_LED_5_PORT, LED_GRP_LED_5_PIN);
    DL_GPIO_setPins(LED_GRP_LED_6_PORT, LED_GRP_LED_6_PIN);
    DL_GPIO_setPins(LED_GRP_LED_7_PORT, LED_GRP_LED_7_PIN);
    DL_GPIO_setPins(LED_GRP_LED_8_PORT, LED_GRP_LED_8_PIN);
}

void LED4x4_Init(void)
{
    LED4x4_Clear();
}

void LED4x4_Display(void)
{
    static uint8_t count = 0;
    LED4x4_Clear();
    switch (count) // 4列扫描启动
    {
    case 0:
        DL_GPIO_clearPins(LED_GRP_LED_5_PORT, LED_GRP_LED_5_PIN); // 扫描第一列
        if (led4x4_buffer[0][0])
        {
            DL_GPIO_setPins(LED_GRP_LED_1_PORT, LED_GRP_LED_1_PIN); // 点亮第一列第一个灯
        }
        if (led4x4_buffer[1][0])
        {
            DL_GPIO_setPins(LED_GRP_LED_2_PORT, LED_GRP_LED_2_PIN); // 点亮第一列第二个灯
        }
        if (led4x4_buffer[2][0])
        {
            DL_GPIO_setPins(LED_GRP_LED_3_PORT, LED_GRP_LED_3_PIN); // 点亮第一列第三个灯
        }
        if (led4x4_buffer[3][0])
        {
            DL_GPIO_setPins(LED_GRP_LED_4_PORT, LED_GRP_LED_4_PIN); // 点亮第一列第四个灯
        }
        break;
    case 1:
        DL_GPIO_clearPins(LED_GRP_LED_6_PORT, LED_GRP_LED_6_PIN);// 扫描第二列
        if (led4x4_buffer[0][1])
        {
            DL_GPIO_setPins(LED_GRP_LED_1_PORT, LED_GRP_LED_1_PIN);// 点亮第二列第一个灯
        }
        if (led4x4_buffer[1][1])
        {
            DL_GPIO_setPins(LED_GRP_LED_2_PORT, LED_GRP_LED_2_PIN);// 点亮第二列第二个灯
        }
        if (led4x4_buffer[2][1])
        {
            DL_GPIO_setPins(LED_GRP_LED_3_PORT, LED_GRP_LED_3_PIN);// 点亮第二列第三个灯
        }
        if (led4x4_buffer[3][1])
        {
            DL_GPIO_setPins(LED_GRP_LED_4_PORT, LED_GRP_LED_4_PIN);// 点亮第二列第四个灯
        }
        break;
    case 2:
        DL_GPIO_clearPins(LED_GRP_LED_7_PORT, LED_GRP_LED_7_PIN);// 扫描第三列
        if (led4x4_buffer[0][2])
        {
            DL_GPIO_setPins(LED_GRP_LED_1_PORT, LED_GRP_LED_1_PIN);// 点亮第三列第一个灯
        }
        if (led4x4_buffer[1][2])
        {
            DL_GPIO_setPins(LED_GRP_LED_2_PORT, LED_GRP_LED_2_PIN);// 点亮第三列第二个灯
        }
        if (led4x4_buffer[2][2])
        {
            DL_GPIO_setPins(LED_GRP_LED_3_PORT, LED_GRP_LED_3_PIN);// 点亮第三列第三个灯
        }
        if (led4x4_buffer[3][2])
        {
            DL_GPIO_setPins(LED_GRP_LED_4_PORT, LED_GRP_LED_4_PIN);// 点亮第三列第四个灯
        }
        break;
    case 3:
        DL_GPIO_clearPins(LED_GRP_LED_8_PORT, LED_GRP_LED_8_PIN);// 扫描第四列
        if (led4x4_buffer[0][3])
        {
            DL_GPIO_setPins(LED_GRP_LED_1_PORT, LED_GRP_LED_1_PIN);// 点亮第四列第一个灯
        }
        if (led4x4_buffer[1][3])
        {
            DL_GPIO_setPins(LED_GRP_LED_2_PORT, LED_GRP_LED_2_PIN); // 点亮第四列第二个灯
        }
        if (led4x4_buffer[2][3])
        {
            DL_GPIO_setPins(LED_GRP_LED_3_PORT, LED_GRP_LED_3_PIN); // 点亮第四列第三个灯
        }
        if (led4x4_buffer[3][3])
        {
            DL_GPIO_setPins(LED_GRP_LED_4_PORT, LED_GRP_LED_4_PIN); // 点亮第四列第四个灯
        }
        break;
    default:
        break;
    }
    count = count < 3 ? count + 1 : 0;
}